<?php

namespace App\Repositories;

use App\Lib\ApiResponse;
use App\Lib\WxMapHandle;
use App\Models\Report;
use App\Models\Staff;
use App\Models\Wxusers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class WxMapRepository
{
    const QRCODE_TIME_SEC_CACHE = 3600;
    const PC_LOGIN_STR = 'manage_lg';
    const FOOT_WORK_STR = 'foot_work';

    public function getWxMapQrcode($key,$mall_id=0)
    {
        $result = cache()->get($key);
        if(!empty($result)){
            return ApiResponse::success(json_decode($result,true));
        }
        $app = WxMapHandle::getOfficialAccount($mall_id);

        try {
            $result = $app->qrcode->temporary($key, self::QRCODE_TIME_SEC_CACHE);
//            $result = [
//                'ticket' => uniqid('qrcode:'),
//                'expire_seconds' => self::QRCODE_TIME_SEC_CACHE,
//            ];
        }catch (\Exception $e){
            return ApiResponse::fail('获取二维码失败:'.$e->getMessage());
        }

        if(!isset($result['ticket'])){
            return ApiResponse::fail('获取二维码失败');
        }
        $result['key'] = $key;
        $result['full_url'] = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . urlencode($result['ticket']);

        cache()->set($key, \json_encode($result),$result['expire_seconds']-300);

        return ApiResponse::success($result);
    }

    public function checkWxQrcodeLogin(Request $request)
    {
        $key = $request->get('key');
        if(empty($key)){
            return ApiResponse::fail('参数错误');
        }
        $staff = Staff::query()->where('login_key',$key)->first();
        if(empty($staff)){
            return ApiResponse::fail('待登陆');
        }

        $token = Hash::make(uniqid('admin').$staff->id);
        $staff->login_token = $token;
        $staff->login_key = '';
        $staff->save();

        return ApiResponse::success(['token' => $token]);
    }

    public function saveWxUser($wxuser_info,$mall_id)
    {
        $wxuser = Wxusers::updateOrCreate(['wx_openid' => $wxuser_info['id']],[
            'mall_id' => $mall_id,
            'wx_unionid' => $wxuser_info['original']['unionid']??'',
            'wx_nickname' => $wxuser_info['nickname']??'',
            'wx_headimgurl' => $wxuser_info['avatar']??'',
            'wx_sex' => $wxuser_info['original']['sex']??0,
            'wx_province' => $wxuser_info['original']['province']??'',
            'wx_city' => $wxuser_info['original']['city']??'',
        ]);

        return $wxuser;
    }

    public function syncWxUserInfoToStaff(Wxusers $wxuser)
    {
        $staff  = Staff::query()->where('wx_openid', $wxuser->wx_openid)->first();
        if(empty($staff)){
            return ApiResponse::fail('用户不存在');
        }

        $staff->wx_unionid = $wxuser->wx_unionid;
        $staff->wx_nickname = $wxuser->wx_nickname;
        $staff->wx_headimgurl = $wxuser->wx_headimgurl;
        $staff->save();

        return ApiResponse::success();
    }

    public function syncWxUserInfoToReport(Wxusers $wxuser)
    {
        $report  = Report::query()->where('wx_openid', $wxuser->wx_openid)->first();
        if(empty($report)){
            return ApiResponse::fail('报告不存在');
        }
        $report->wxuser_id = $wxuser->id;
        $report->save();

        return ApiResponse::success();
    }
}
